package com.ld.control.utils;

import com.alibaba.fastjson.JSONObject;
import com.ld.control.exception.BusinessException;
import com.ld.control.sysenum.ResultResponseEnum;
import kotlin.jvm.internal.MagicApiIntrinsics;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
public class HttpUtils {

    /**
     * 发送post请求
     * @param url
     * @param jsonParam
     * @param connectTimeoutSeconds
     * @return
     */
    public static String sendPostConnectTimeout(String url, String jsonParam, Long connectTimeoutSeconds, Long readTimeoutSeconds) {
        //log.info("sendPost request url:{},jsonParam：{}", url, jsonParam);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(connectTimeoutSeconds, TimeUnit.SECONDS)
                .callTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
                .build();
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonParam);
            Request request = new Request.Builder().url(url).post(requestBody).build();
            Response response = client.newCall(request).execute();
            return Objects.requireNonNull(response.body()).string();
        } catch (SocketTimeoutException socketTimeoutException){
            throw new BusinessException(ResultResponseEnum.REQ_TIMEOUT);
        }catch (InterruptedIOException interruptedIOException){
            log.error("sendPost error: ", interruptedIOException);
            log.info("sendPost error url:{}->jsonParam:{}", url, jsonParam);
            throw new BusinessException(ResultResponseEnum.RESP_TIMEOUT);
        } catch (Exception exception) {
            log.error("sendPost error: ", exception);
            throw new BusinessException("请求异常，请检查设备通讯是否正常");
        }
    }

    /**
     * 发送bodu体的Post请求
     * @param url
     * @param jsonParam
     * @return
     */
    public static String sendPost(String url, String jsonParam) {
        return sendPostConnectTimeout(url, jsonParam, 5L, 30L);
    }

    public static String setGet(String url) {
        return setGetConnectTimeout(url, 5L, 5L);
    }


    /**
     * 发送get请求
     * @param url
     * @return
     */
    public static String setGetConnectTimeout(String url, Long connectTimeoutSeconds, Long readTimeoutSeconds) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(connectTimeoutSeconds, TimeUnit.SECONDS)
                .callTimeout(readTimeoutSeconds, TimeUnit.SECONDS)
                .build();
        try {
            Request request = new Request.Builder().url(url).build();
            Response response = client.newCall(request).execute();
            String responseData = Objects.requireNonNull(response.body()).string();
            //log.info("setGet responseData: " + responseData);
            return responseData;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

}
